/// Autor : Frank Luther
/// 
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Imaging;
using System.Data;
using System.Windows.Forms;

namespace EForms
{
	/// <summary>
	/// Summary description for AnimatedPictureBox.
	/// </summary>
	public class AnimatedPictureBox : System.Windows.Forms.Control
	{
		/// <summary> 
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		private EventHandler	nextFrameEventHandler ;
		private bool			animate = false;

		private Timer			timer;
		private int				roundTrips = 0;
		private FrameDimension	frameDimension;
		private int				frameCount;

		private int				actualframe = 0;
		private Image			origionalImage;

		/// <summary>
		/// The Image to show
		/// <remarks> Beware the Control changes it's size to the Image size </remarks>
		/// </summary>
		public Image Image
		{
			get
			{
				return this.origionalImage;
			}
			set
			{				
				this.origionalImage		= value;
//				this.StopAnimation();
				if (this.origionalImage != null)
				{
					this.Size = this.origionalImage.Size;
					object o = this.origionalImage.FrameDimensionsList.GetValue(0);
					if (o != null)
					{
						Guid fdGuid = (Guid) o;
								
						this.frameDimension = new FrameDimension(fdGuid);
						this.frameCount = this.Image.GetFrameCount(this.frameDimension);
					
					}
				}
			}
		}
		/// <summary>
		/// 
		/// </summary>
		public AnimatedPictureBox()
		{			
			// This call is required by the Windows.Forms Form Designer.
			InitializeComponent();			
			this.nextFrameEventHandler = new EventHandler(this.OnNextImageFrame);		
		}

		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Component Designer generated code
		/// <summary> 
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			components = new System.ComponentModel.Container();
		}
		#endregion

		/// <summary>
		/// 
		/// </summary>
		/// <param name="e"></param>
		protected override void OnPaint(PaintEventArgs e)
		{
			base.OnPaint(e);
			using (Graphics g = e.Graphics)
			{				
				if (this.animate == true)
				{
					if (this.origionalImage != null )
					{
						this.actualframe++;
						ImageAnimator.UpdateFrames(this.origionalImage);
					}
				}				
				g.DrawImage(this.origionalImage, this.ClientRectangle);				
			}
			if (this.actualframe % this.frameCount == 0 && this.roundTrips > 0)
			{
				if (this.roundTrips > 0)
				{
					this.roundTrips--;
				}
				if (this.roundTrips == 0)
				{
					this.StopAnimation();
				}
			}
		}
		/// <summary>
		/// Plays the Animation endless
		/// </summary>
		public void StartAnimation()
		{
			this.animate = true;
			if (ImageAnimator.CanAnimate(this.origionalImage) == true)
			{
				ImageAnimator.Animate(this.origionalImage, this.nextFrameEventHandler);
			}
		}
		/// <summary>
		/// Plays the Animation for the given time span
		/// </summary>
		/// <param name="timeToPlay"></param>
		public void StartAnimation(TimeSpan timeToPlay)
		{			
			this.timer = new Timer();
			this.timer.Interval = (int)timeToPlay.TotalMilliseconds;
			this.timer.Tick += new EventHandler(this.OnTick);
			this.timer.Enabled = true;
			this.StartAnimation();
		}
		/// <summary>
		///	 Plays the animation "roundtrip" times
		/// </summary>
		/// <param name="roundtrips">Times to play (0 is endless)</param>
		public void StartAnimation(int roundtrips)
		{
			this.roundTrips = roundtrips;
			this.StartAnimation();
		}
		/// <summary>
		/// Stops the Animation immideately
		/// </summary>
	
		public void StopAnimation()
		{
			this.roundTrips = 0;			
			if (ImageAnimator.CanAnimate(this.origionalImage) == true)
			{
				if (this.origionalImage.FrameDimensionsList.Length > 0)
				{
					this.origionalImage.SelectActiveFrame(this.frameDimension, 0);
					this.actualframe = 0;
				}
				ImageAnimator.StopAnimate(this.origionalImage, this.nextFrameEventHandler);
				this.Invalidate();
			}
			this.animate = false;
		}
		/// <summary>
		/// Eventhandler of the timer
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		protected void OnTick(object sender, EventArgs e)
		{
			this.StopAnimation();
			this.timer.Enabled = false;
			this.timer.Tick -= new EventHandler(this.OnTick);
			this.timer.Dispose();
			this.timer = null;
		}
		/// <summary>
		/// Eventhandler of the ImageAnimtor
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		protected void OnNextImageFrame(object sender, EventArgs e)
		{
			this.Invalidate();
		}
	}
}



